package com.tg.app.widget;

import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.util.LogUtils;
import com.tg.appcommon.android.TGLog;

/**
 * Describe:
 * Created by Rance Rang on 2019/2/21.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class ACLinearLayoutManager extends LinearLayoutManager {
    private static final String TAG = "ACLinearLayoutManager";
    private final float mShrinkAmount = 0.15f;
    private final float mShrinkDistance = 0.9f;
    private boolean isScrollEnabled = true;

    public ACLinearLayoutManager(Context context) {
        super(context);
    }

    public ACLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public ACLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void smoothScrollToPosition(RecyclerView recyclerView, int position, int snapPreference) {
        //Log.d(TAG, "smoothScrollToPosition");

        smoothScrollToPositionWithOffset(recyclerView, position, snapPreference, 0);
    }

    public void smoothScrollToPositionWithOffset(RecyclerView recyclerView, final int position, final int snapPreference, final int offset) {
        //Log.d(TAG, "smoothScrollToPositionWithOffset");
        if (position < 0){
            return;
        }
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    @Override
                    public PointF computeScrollVectorForPosition(int targetPosition) {
                        return ACLinearLayoutManager.this
                                .computeScrollVectorForPosition(targetPosition);
                    }

                    @Override
                    public int calculateDyToMakeVisible(View view, int snapPreference) {
                        return super.calculateDyToMakeVisible(view, snapPreference) + offset;
                    }

                    /**
                     * MILLISECONDS_PER_INCH 默认为25，及移动每英寸需要花费25ms，如果你要速度变快一点，就直接设置设置小一点，注意这里的单位是f
                     */

                    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                        return 25f / displayMetrics.densityDpi;
                    }

                    @Override
                    protected int getVerticalSnapPreference() {
                        return snapPreference;
                    }
                };

        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            TGLog.e(TAG, "meet a IOOBE in RecyclerView");
        }
    }


    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int scrolled = super.scrollVerticallyBy(dy, recycler, state);
        return scrolled;

    }

    public void setScrollEnabled(boolean flag) {
        isScrollEnabled = flag;
    }

    @Override
    public boolean canScrollVertically() {
        return isScrollEnabled && super.canScrollVertically();
    }

}
